﻿Public Class StuScore

    Dim examId As Integer = -1

    Public Property CheckExamId As Integer
        Get
            Return examId
        End Get
        Set(value As Integer)
            examId = value
        End Set
    End Property

    Private Sub StuScore_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: 这行代码将数据加载到表“ExamDataSet.view_stu_score”中。您可以根据需要移动或删除它。
        'Me.View_stu_scoreTableAdapter.Fill(Me.ExamDataSet.view_stu_score)
        'TODO: 这行代码将数据加载到表“ExamDataSet.score”中。您可以根据需要移动或删除它。
        ' Me.ScoreTableAdapter.Fill(Me.ExamDataSet.score)
        If examId <> -1 Then
            Try
                Me.View_stu_scoreTableAdapter1.FillByExamId(Me.ExamDataSet.view_stu_score, examId)
                Me.ExamTableAdapter.FillById(Me.ExamDataSet.exam, examId)
                Dim totalScore As Integer = Me.Exam_questionsTableAdapter1.GetScore(examId)
                lblTotalScore.Text = totalScore
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
        Dim r As DataGridViewRow
        If DataGridView1.RowCount >= 1 Then
            For Each r In DataGridView1.Rows
                r.Cells("checkDetails").Value = "查看详细"
                r.Cells("checkDetails").Tag = r.Cells("stu_id").Value
            Next r
        End If
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Dim stuName As String = "%" + txtStuName.Text + "%"
        Try
            Me.View_stu_scoreTableAdapter1.FillByExamIdAndStuName(Me.ExamDataSet.view_stu_score, examId, stuName)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnExport_Click(sender As Object, e As EventArgs) Handles btnExport.Click
        Dim sfd As New SaveFileDialog()
        Dim examName As String = lblExamName.Text
        sfd.Filter = "Excel 文件（.xls)|*.xls"
        sfd.Title = "保存到Excel文件"
        sfd.ShowDialog()
        ' If the file name is not an empty string open it for saving.
        If Not String.IsNullOrEmpty(sfd.FileName) Then
            Try
                Dim xlApp As Excel.Application
                Dim xlBook As Excel.Workbook
                Dim xlSheet As Excel.Worksheet
                xlApp = CreateObject("Excel.Application")
                xlBook = xlApp.Workbooks.Add   '创建工作簿  
                xlApp.Visible = False
                xlSheet = xlBook.Worksheets(1) '创建工作表  
                xlSheet.Name = "学生成绩" '表名  
                xlSheet.Cells(1, 1) = "学生姓名"
                xlSheet.Columns.ColumnWidth = 25
                xlSheet.Cells(1, 2) = "试卷名称"
                xlSheet.Cells(1, 3) = "考试时间"
                xlSheet.Cells(1, 4) = "考试分数"
                Dim rowCount As Integer = 2
                Dim row As ExamDataSet.view_stu_scoreRow
                For Each row In Me.ExamDataSet.view_stu_score
                    xlSheet.Cells(rowCount, 1) = row.name
                    xlSheet.Cells(rowCount, 2) = examName
                    xlSheet.Cells(rowCount, 3) = row.create_time.ToLongDateString()
                    xlSheet.Cells(rowCount, 4) = row.score.ToString()

                    rowCount = rowCount + 1
                Next

                xlSheet.SaveAs(sfd.FileName)
                xlBook.Close(True) '关闭工作簿  
                xlApp.Quit() '结束EXCEL对象  
                xlApp = Nothing '释放xlApp对象
                MessageBox.Show("导出成功。")
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If e.ColumnIndex = 7 And e.RowIndex >= 0 Then
            Dim rowIndex As Integer = e.RowIndex
            Dim stuId As Integer = Integer.Parse(DataGridView1.SelectedCells(0).Tag.ToString())
            Dim stuName As String = DataGridView1.Rows(rowIndex).Cells("stuName").Value.ToString()
            Dim stuScore As String = DataGridView1.Rows(rowIndex).Cells("studentScore").Value.ToString()
            Dim stuExTime As String = DataGridView1.Rows(rowIndex).Cells("studentExamTime").Value.ToString()
            Dim stuAns As StuAnswerDisplay = New StuAnswerDisplay()
            stuAns.StudId = stuId
            stuAns.ExamsId = examId
            stuAns.ExamsName = lblExamName.Text
            stuAns.ExamsScore = lblTotalScore.Text
            stuAns.StudName = stuName
            stuAns.StudScore = stuScore
            stuAns.StuJoinTime = stuExTime
            stuAns.ShowDialog()
        End If

    End Sub
End Class